package com.ldj.struts.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemSession;
import com.ldj.common.Upload;
import com.ldj.hibernate.AnnualVeriRepEndDate;
import com.ldj.hibernate.AnnualVeriRepEndDateDAO;
import com.ldj.hibernate.EmployerContractInfo;
import com.ldj.hibernate.EmployerContractInfoDAO;
import com.ldj.hibernate.EmployerInfo;
import com.ldj.hibernate.EmployerInfoDAO;
import com.ldj.hibernate.UnitInfoDAO;
import com.ldj.hibernate.UnitUserDAO;
import com.ldj.hibernate.UnitUser;
import com.ldj.struts.form.EmployerContractInfoForm;

public class UnitContractInfoAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoForm eForm = (EmployerContractInfoForm)form;
		//查出单位id
		String loginUnitId = SystemSession.getUnitUserId(request);
		UnitUserDAO unitInfoDao = new UnitUserDAO(request, response);
		unitInfoDao.getSession().flush();
		unitInfoDao.getSession().clear();
		UnitUser uid = unitInfoDao.findId(loginUnitId);
		if(uid.getUnitUser() != null){
			String unitId = uid.getUnitUser().getEmpInfoId();

	//		查找年审结束时间
			AnnualVeriRepEndDateDAO annuDao = new AnnualVeriRepEndDateDAO(request, response);
			AnnualVeriRepEndDate annuDate = annuDao.find();
			request.setAttribute("date", annuDate);
			//查询单位合同信息是否过期或者被解除
			EmployerContractInfoDAO ecDao = new EmployerContractInfoDAO(request, response);
			//EmployerContractInfo ecInfo = ecDao.find(unitId);
			List list = ecDao.findAllPage(unitId, eForm, null);
			request.setAttribute("list", list);
			//request.setAttribute("ecInfo", ecInfo);
			return mapping.findForward("list");
		}
		return mapping.findForward("Applying");
	}
	
	
	public ActionForward upComingList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoForm eForm = (EmployerContractInfoForm)form;
		//查出单位id
		String loginUnitId = SystemSession.getUnitUserId(request);
		UnitInfoDAO unitInfoDao = new UnitInfoDAO(request, response);
		unitInfoDao.getSession().flush();
		unitInfoDao.getSession().clear();
		UnitUser uid = unitInfoDao.findId(loginUnitId);
		
		if(uid.getUnitUser() != null){
			String unitId = uid.getUnitUser().getEmpInfoId();
			EmployerContractInfoDAO ecDao = new EmployerContractInfoDAO(request, response);
			List list = ecDao.findUpComContrByUnitId(unitId, eForm);
			request.setAttribute("list", list);
			return mapping.findForward("upComingList");
		}
		return mapping.findForward("Applying");
	}

	public ActionForward upComingDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		dao.getSession().flush();
		dao.getSession().clear();
		EmployerContractInfo ecinfo = dao.findById(request.getParameter("ecid"));
		//单位信息
		EmployerInfoDAO dao2 = new EmployerInfoDAO(request, response);
		EmployerInfo einfo = dao2.findById(ecinfo.getEmployerInfo().getEmpInfoId());
		request.setAttribute("eInfo", einfo);
		request.setAttribute("ecInfo", ecinfo);
		return mapping.findForward("upComingdetail");
	}
	
	
	
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		dao.getSession().flush();
		dao.getSession().clear();
		EmployerContractInfo ecinfo = dao.findById(request.getParameter("ecid"));
		//单位信息
		EmployerInfoDAO dao2 = new EmployerInfoDAO(request, response);
		EmployerInfo einfo = dao2.findById(ecinfo.getEmployerInfo().getEmpInfoId());
		request.setAttribute("eInfo", einfo);
		request.setAttribute("ecInfo", ecinfo);
		return mapping.findForward("detail");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AnnualVeriRepEndDateDAO annuDao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuDate = annuDao.find();
		Date a = new Date();
		Date b = annuDate.getAnnualVeriRepEndDate();
		if(a.before(b)) {
			return mapping.findForward("toAdd");
		}
		return new ActionForward("/UnitContractInfo.do?method=list", true);
	}
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AnnualVeriRepEndDateDAO annuDao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuDate = annuDao.find();
		Date a = new Date();
		Date b = annuDate.getAnnualVeriRepEndDate();
		if(a.before(b)) {
			EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
			EmployerContractInfo info = dao.findById(request.getParameter("ecid"));
			request.setAttribute("eInfo", info);	
			return mapping.findForward("toUpdate");
		}
		return new ActionForward("/UnitContractInfo.do?method=list", true);
	}
	
	public ActionForward contractGo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {		
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		EmployerContractInfo info = dao.findById(request.getParameter("ecid"));
		info.setEmpConState("1");
		dao.merge(info);
		info.setEmpConContractType("1");
		info.setEmpConContractBeginTime(info.getEmpConContractEndTime());
		request.setAttribute("ecInfo", info);
		return mapping.findForward("toAdd");
		}
	
//	public ActionForward contractGo(ActionMapping mapping, ActionForm form,
//			HttpServletRequest request, HttpServletResponse response) {		
//		EmployerContractInfoForm eForm = (EmployerContractInfoForm)form;
//		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
//		EmployerContractInfo info = dao.findById(eForm.getEmpConId());
//		info.setEmpConContractEndTime(eForm.getDateEmpConContractEndTime());
//		info.setEmpConState("1");
//		dao.merge(info);
//		request.setAttribute("cInfo", "OK");
//		return mapping.findForward("contractGo");
//	}
	
	public ActionForward contractEnd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoForm eForm = (EmployerContractInfoForm)form;
		AnnualVeriRepEndDateDAO annuDao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuDate = annuDao.find();
		Date a = new Date();
		Date b = annuDate.getAnnualVeriRepEndDate();
		if(a.before(b)) {
			EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
			EmployerContractInfo info = dao.findById(eForm.getEmpConId());
			info.setEmpConRecontractendTime(eForm.getDateEmpConRecontractendTime());
			info.setEmpConState("2");
			info.setEmpConEndCouse(eForm.getEmpConEndCouse());
			dao.merge(info);
		}
		request.setAttribute("cInfo", "OK");
		return mapping.findForward("contractGo");
		//return new ActionForward("/UnitContractInfo.do?method=list", true);
	}

	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoForm eForm = (EmployerContractInfoForm)form;
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		EmployerContractInfo info = dao.findById(request.getParameter("ecid"));
		info.setEmpConEmpName(eForm.getEmpConEmpName());
		info.setEmpConEmpSex(eForm.getEmpConEmpSex());
		info.setEmpConIdCardNum(eForm.getEmpConIdCardNum());
		info.setEmpConEducationLevel(eForm.getEmpConEducationLevel());
		info.setEmpConHouse(eForm.getEmpConHouse());
		info.setEmpConAddress(eForm.getEmpConAddress());
		info.setEmpConPhone(eForm.getEmpConPhone());
		info.setEmpConWordType(eForm.getEmpConWordType());
		info.setEmpConWorkType(eForm.getEmpConWorkType());
		info.setEmpConCompetence(eForm.getEmpConCompetence());
		info.setEmpConEmpDate(eForm.getDateEmpConEmpDate());
		info.setEmpConContractTime(eForm.getEmpConContractTime());
		info.setEmpConContractBeginTime(eForm.getDateEmpConContractBeginTime());
		info.setEmpConContractEndTime(eForm.getDateEmpConContractEndTime());
		info.setEmpConPost(eForm.getEmpConPost());
		info.setEmpConContractFileUrl(eForm.getEmpConContractFileUrl());
		dao.merge(info);
		return new ActionForward("/UnitContractInfo.do?method=list", true);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoForm eForm = (EmployerContractInfoForm)form;
		EmployerContractInfo info = new EmployerContractInfo();
		info.setEmpConEmpName(eForm.getEmpConEmpName());
		info.setEmpConEmpSex(eForm.getEmpConEmpSex());
		info.setEmpConIdCardNum(eForm.getEmpConIdCardNum());
		info.setEmpConEducationLevel(eForm.getEmpConEducationLevel());
		info.setEmpConHouse(eForm.getEmpConHouse());
		info.setEmpConAddress(eForm.getEmpConAddress());
		info.setEmpConPhone(eForm.getEmpConPhone());
		info.setEmpConWordType(eForm.getEmpConWordType());
		info.setEmpConWorkType(eForm.getEmpConWorkType());
		info.setEmpConCompetence(eForm.getEmpConCompetence());
		info.setEmpConEmpDate(eForm.getDateEmpConEmpDate());
		info.setEmpConContractTime(eForm.getEmpConContractTime());
		info.setEmpConContractBeginTime(eForm.getDateEmpConContractBeginTime());
		info.setEmpConContractEndTime(eForm.getDateEmpConContractEndTime());
		info.setEmpConPost(eForm.getEmpConPost());
		info.setEmpConContractFileUrl(eForm.getEmpConContractFileUrl());
		info.setEmpConDelOper("0");
		if(eForm.getEmpConContractType() != null && !"".equals(eForm.getEmpConContractType())){
			info.setEmpConContractType(eForm.getEmpConContractType());
			info.setEmpConState("0");
		}else{
			info.setEmpConContractType("0");
			info.setEmpConState("0");
		}
		UnitUserDAO unitInfoDao = new UnitUserDAO(request, response);
		String loginId = SystemSession.getUnitUserId(request);
		UnitUser uid = unitInfoDao.findId(loginId);
		String unitId = uid.getUnitUser().getEmpInfoId();
		EmployerInfoDAO edao = new EmployerInfoDAO(request, response);
		EmployerInfo eInfo = edao.findById(unitId);
		info.setEmployerInfo(eInfo);
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		dao.save(info);
		return new ActionForward("/UnitContractInfo.do?method=list", true);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AnnualVeriRepEndDateDAO annuDao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuDate = annuDao.find();
		Date a = new Date();
		Date b = annuDate.getAnnualVeriRepEndDate();
		if(a.before(b)) {
			EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
			EmployerContractInfo info = dao.findById(request.getParameter("ecid"));
			info.setEmpConDelOper("1");
			dao.merge(info);
		}
		return new ActionForward("/UnitContractInfo.do?method=list", true);
	}
	
		
	public ActionForward upload(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			HashMap map = Upload.upload(request, "contract", ".jpg;.gif");
			if(map.containsKey("fileName")){
				request.setAttribute("fileName", (String)map.get("fileName"));
			}else{
				request.setAttribute("error", "error");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mapping.findForward("upload");
	}
}